﻿using IceCS.HotLoad.Modules.Interfaces;
using System;
using System.Text;

namespace IceCS.HotLoad.Cmd
{
    class Program
    {
        public static ILogPrinter printer;
        public static HotLoadManager loadManager;

        static Program()
        {
            loadManager = new HotLoadManager();
        }

        public static string GetInput()
        {
            return Console.ReadLine();
        }

        public static void LoadLogPrinter()
        {
            //加载程序集
            var result = loadManager.LoadDll("IceCS.HotLoad.Modules.LogPrinter.dll");
            if (result)
            {
                Console.WriteLine("加载程序集成功");
            }
            else
            {
                Console.WriteLine("加载程序集失败");
            }
            printer = loadManager.CreateInstance<ILogPrinter>(t => t.Name == "ConsoleLogPrinter");
        }

        static void Main(string[] args)
        {

            try
            {
                LoadLogPrinter();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            while (true)
            {
                try
                {
                    Console.WriteLine("please input :");
                    var input = GetInput().Trim();
                    var action = string.Empty;
                    var others = string.Empty;
                    if (input.Length == 0)
                    {
                        action = "hi";
                    }
                    else
                    {
                        var firstSpaceIdx = input.IndexOf(" ");
                        if (firstSpaceIdx == -1)
                        {
                            action = input;
                        }
                        else
                        {
                            action = input.Substring(0, firstSpaceIdx);
                            others = input.Substring(firstSpaceIdx);
                        }
                    }
                    switch (action)
                    {
                        case "hi":
                            Console.WriteLine("hi");
                            break;
                        case "print":
                        case "echo":
                            printer.Print(others);
                            break;
                        case "reload":
                            LoadLogPrinter();
                            break;
                        default:
                            var sb = new StringBuilder();
                            sb.AppendLine("error!");
                            sb.AppendLine("you can type this command:");
                            sb.AppendLine("* hi , just print hi");
                            sb.AppendLine("* echo/print , print the text after this command ,e.g. echo goodday , good boy");
                            sb.AppendLine("* reload ,  it can reload dll file ,then echo style will change, and the app don't need to close and reopn");
                            Console.WriteLine(sb.ToString());
                            break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}
